package com.readpost.mapper;

import com.readpost.doman.Entity.ArticleCategories;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 文章分类Mapper
 */
@Mapper
public interface ArticleCategoriesMapper {

    //增加文章分类
    @Insert("insert into article_categories(category_name,description,created_at,updated_at)" +
            "values(#{categoryName},#{description},now(),now())")
    void addCategory(ArticleCategories category);

    //根据分类名称查询分类是否存在
    @Select("select IFNULL(count(*), 0) from article_categories where category_name = #{categoryName}")
    int countCategoryByName(ArticleCategories categoryName);

    //根据分类Id查询分类是否存在
    @Select("select IFNULL(count(*), 0) from article_categories where category_id = #{categoryId}")
    int countCategoryById(Integer categoryId);

    //获取所有文章分类
    @Select("select * from article_categories")
    List<ArticleCategories> getAllCategory();

    //根据分类Id查询分类
    @Select("select * from article_categories where category_id = #{categoryId}")
    ArticleCategories getCategoryById(Integer categoryId);

    //根据分类Id删除分类
    @Select("delete from article_categories where category_id = #{categoryId}")
    void deleteCategoryById(Integer categoryId);


    //根据id修改文章分类
    @Select("update article_categories set category_name = #{categoryName}, description = #{description}, updated_at = now() where category_id = #{categoryId}")
    void updateCategory(ArticleCategories category);
}
